package com.yayaedu;

import cn.hutool.crypto.SecureUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author YAYA
 * @create 2023-01-01-18:32
 */
public class UserDao {

    /**
     * 查询所有用户
     * @return
     */
    public List<User> selectAllUser(){
        List<User> userList = new ArrayList<User>();
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        conn = DbUtilHikari.getConnection();
        String sql= "SELECT * FROM users";
        try {
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while (rs.next()){
                User user = new User();
                user.setUserid(rs.getInt("userid"));
                user.setUsername(rs.getString("username"));
                user.setUserpsw(rs.getString("userpsw"));
                userList.add(user);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DbUtilHikari.close(rs,pstmt,conn);
        }
        return userList;
    }
    /**
     * 按照用户编号修改用户
     * @param userid
     * @param uname
     * @param upsw
     * @return
     */
    public int updateUser(int userid,String uname,String upsw){
        // 1. 声明一些变量备用
        // conn代表数据库连接
        Connection conn = null;
        // 预编译对象，用pstmt执行SQL语句
        PreparedStatement pstmt = null;
        // 注册成功与否的标志，如果是0注册失败；如果1注册成功
        int flag = 0;

        // 2. 获取连接
        conn = DbUtilHikari.getConnection();
        // 3. 定义并准备SQL语句
        String sql = "UPDATE users SET username=?,userpsw=? WHERE userid=?";
        try {
            pstmt = conn.prepareStatement(sql);
            //4. 给?赋值
            pstmt.setString(1,uname);
            pstmt.setString(2,SecureUtil.md5(upsw));
            pstmt.setInt(3,userid);
            // 5. 执行SQL语句（下达执行指令）
            flag = pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DbUtilHikari.close(pstmt,conn);
        }
        return flag;
    }
    /**
     * 按用户编号删除用户
     * @param userid
     * @return
     */
    public int deleteUser(int userid){
        int flag = 0;
        Connection conn = null;
        PreparedStatement pstmt = null;
        conn = DbUtilHikari.getConnection();
        String sql= "DELETE FROM users WHERE userid=?";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,userid);
            flag = pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DbUtilHikari.close(pstmt,conn);
        }
        return flag;
    }
    /**
     * 按用户名和密码登录
     * @param uname
     * @param upsw
     * @return
     */
    public boolean login(String uname,String upsw) {
        boolean flag = false;
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        conn = DbUtilHikari.getConnection();
        String sql= "SELECT * FROM users WHERE username=? AND userpsw=?";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,uname);
            pstmt.setString(2, SecureUtil.md5(upsw));
            rs = pstmt.executeQuery();
            if(rs.next()){
                flag=true;
            }else {
                flag=false;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DbUtilHikari.close(rs,pstmt,conn);
        }
        return flag;
    }
    /**
     * 用户注册
     * @param uname 用户名
     * @param upsw 密码
     * @return 注册成功返回1，否则返回0
     */
    public int insertUser(String uname,String upsw) {
        // 1. 声明一些变量备用
        // conn代表数据库连接
        Connection conn = null;
        // 预编译对象，用pstmt执行SQL语句
        PreparedStatement pstmt = null;
        // 注册成功与否的标志，如果是0注册失败；如果1注册成功
        int flag = 0;

        // 2. 获取连接
        conn = DbUtilHikari.getConnection();
        // 3. 定义并准备SQL语句
        String sql = "INSERT INTO users(username,userpsw) VALUES(?,?)";
        try {
            pstmt = conn.prepareStatement(sql);
            //4. 给?赋值
            pstmt.setString(1,uname);
            pstmt.setString(2,SecureUtil.md5(upsw));
            // 5. 执行SQL语句（下达执行指令）
            flag = pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            // 6. 关闭数据库资源
            DbUtilHikari.close(pstmt,conn);
        }
        return flag;
    }
}
